# from threading import Thread
# import time
# x = 100
#
# def task():
#     global x
#     temp = x
#     time.sleep(1)
#     temp -= 1
#     x = temp
#
#
#
# if __name__ == '__main__':
#     t = Thread(target=task)
#     t.start()
#     t.join()
#     print(f'主线程{x}')



# 开启100个线程

# from threading import Thread
# import time
# x = 100
#
# def task():
#     global x
#     temp = x
#     time.sleep(1)
#     temp -= 1
#     x = temp
#
#
#
# if __name__ == '__main__':
#     t_l = []
#     for i in range(100):
#         t = Thread(target=task)
#         t_l.append(t)
#         t.start()
#
#     for i in t_l:
#         i.join()
#
#     print(f'主线程{x}')


# 加锁

# from threading import Thread
# from threading import Lock
# import time
# x = 100
#
# def task(lock):
#     global x
#     lock.acquire()
#     temp = x
#     # time.sleep(0.1)
#     temp -= 1  #　99
#     x = temp  #　99
#     lock.release()
# '''
# 第一个线程: x = 100  剩下的线程拿到的也是 x
# 第一个线程执行完毕: x = 99
# 第二个线程进去:temp = 99
# '''
#
#
# if __name__ == '__main__':
#     lock = Lock()
#     t_l = []
#     for i in range(100):
#         t = Thread(target=task,args=(lock,))
#         t_l.append(t)
#         t.start()
#
#     for i in t_l:
#         i.join()
#
#     print(f'主线程{x}')

# 互斥锁与join区别?
# 互斥锁 随机抢锁,公平. join 提前排好顺序,不公平.但是都是串行.